﻿using UtilLib;

namespace SvGame.Logic;

public static partial class GameLogic
{
    #region Player

    public static void MessageErr(this Player player, EProtocolResult ecode, bool pop = false)
    {
        var ntf = new G2CNtfMudErr() { Pop = pop };
        player.Send(ntf, ecode);
    }
    public static void MessageNone(this Player player, string text)
    {
        var ntf = new G2CNtfMudMsg() { Msg = new() };
        ntf.Msg.Mode = (int)EMudColorMode.Color;
        ntf.Msg.Color = (int)EMudColor.GRN;
        ntf.Msg.Text = text;
        player.Send(ntf);
    }
    public static int MessageError(this Player player, string text)
    {
        var ntf = new G2CNtfMudMsg() { Msg = new() };
        ntf.Msg.Mode = (int)EMudColorMode.Color;
        ntf.Msg.Color = (int)EMudColor.HIW;
        ntf.Msg.Text = text;
        return player.Send(ntf);
    }
    public static void Message(this Player player, string text)
    {
        var ntf = new G2CNtfMudMsg() { Msg = new() };
        ntf.Msg.Mode = (int)EMudColorMode.Normal;
        ntf.Msg.Text = text;
        player.Send(ntf);
    }
    public static void Message(this Player player, string text, EMudColor color)
    {
        var ntf = new G2CNtfMudMsg() { Msg = new() };
        ntf.Msg.Mode = (int)EMudColorMode.Color;
        ntf.Msg.Color = (int)color;
        ntf.Msg.Text = text;
        player.Send(ntf);
    }

    #endregion

    #region Role

    public static int MessageError(this IRole me, string text)
    {
        if (me is Player)
        {
            (me as Player)?.MessageError(text);
        }
        return 1;
    }
    public static void Message(this IRole me, string text)
    {
        if (me is Player)
        {
            (me as Player)?.Message(text);
        }
    }
    public static void Message(this IRole me, string text, EMudColor color)
    {
        if (me is Player)
        {
            (me as Player)?.Message(text, color);
        }
    }

    #endregion

    #region Room 房间广播
    public static void Message(this Room room, string text, Player exclude = null)
    {
        if (exclude == null)
        {
            foreach (var pp in room.DictPlayer.Values)
            {
                pp.Message(text);
            }
        }
        else
        {
            Message(room, text, new Player[] { exclude });
        }
    }
    public static void Message(this Room room, string text, IEnumerable<Player> listexclude)
    {
        if (listexclude == null)
        {
            foreach (var pp in room.DictPlayer.Values)
            {
                pp.Message(text);
            }
        }
        else
        {
            var listpuid = (from it in listexclude
                            select it.Puid);
            foreach (var pp in room.DictPlayer.Values)
            {
                if (listpuid.Contains(pp.Puid))
                {
                    continue;
                }
                pp.Message(text);
            }
        }
    }
    public static void Message<T>(this Room room, T msg) where T : gpb.IMessage
    {
        foreach (var pp in room.DictPlayer.Values)
        {
            pp.Send(msg);
        }
    }

    #endregion

    #region 视野

    public static void MessageVision(string msg, IRole me, IRole you = null)
    {
        string str1, str2, str3;
        var my_name = me.Name;
        var my_gender = me.Gender;

        str1 = msg.Replace("$P", GenderSelf(my_gender));
        str1 = str1.Replace("$N", GenderSelf(my_gender));
        str3 = msg.Replace("$P", my_name);
        str3 = str3.Replace("$N", my_name);

        Room room = me.Room;
        Player pme = null;
        Player pyou = null;
        if (you != null)
        {
            var your_name = you.Name;
            var your_gender = you.Gender;
            if (you is Player)
            {
                pyou = (you as Player);
                room = pyou.Room;
                str2 = msg.Replace("$P", GenderPronoun(my_gender));
                str2 = str2.Replace("$p", GenderSelf(your_gender));
                str2 = str2.Replace("$N", my_name);
                str2 = str2.Replace("$n", GenderSelf(your_gender));
                pyou.Message(str2);
            }
            str1 = str1.Replace("$p", GenderPronoun(your_gender));
            str1 = str1.Replace("$n", your_name);
            str3 = str3.Replace("$p", your_name);
            str3 = str3.Replace("$n", your_name);
        }
        if (me is Player)
        {
            pme = (me as Player);
            pme?.Message(str1);
        }

        if (room != null)
        {
            var listexclude = new List<Player>();
            if (pme != null)
            {
                listexclude.Add(pme);
            }
            if (pyou != null)
            {
                listexclude.Add(pyou);
            }
            room.Message(str3, listexclude);
        }
    }

    public static void MessageVisionCombat(string msgCombat, IRole me, IRole you = null)
    {
        var msg = msgCombat;
        if (me != null)
        {
            if (me.Comb.Weapon != null)
            {
                msg = msg.Replace("$w", me.Comb.Weapon.Cfg.Name);
            }
        }
        if (you != null)
        {
            if (you.Comb.Weapon != null)
            {
                msg = msg.Replace("$W", you.Comb.Weapon.Cfg.Name);
            }
        }
        MessageVision(msg, me, you);
    }

    #endregion

}
